from rest_framework.viewsets import ModelViewSet
from apps.goods.models import SPUSpecification
from apps.meiduo_admin.serializers.specs import SpecModelSerializer
from rest_framework.permissions import IsAdminUser
from apps.meiduo_admin.utils.pagination import MeiduoPagination
from rest_framework.generics import ListAPIView, CreateAPIView
from apps.meiduo_admin.serializers.specs import SpecOptionSerializer
from apps.goods.models import SpecificationOption
from apps.meiduo_admin.serializers.specs import SpecOptSerializer, SpecSimpleSerializer, SpecSaveSerializer


class SpecViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]
    queryset = SPUSpecification.objects.all()
    serializer_class = SpecModelSerializer
    pagination_class = MeiduoPagination


class SpecOptionView(ListAPIView):
    def get_queryset(self):
        # 需要从路径中接收spu_id，用于查询制定spu的所有规格
        # self.kwargs===>获得路径中的关键字参数
        spu_id = self.kwargs.get('pk')
        return SPUSpecification.objects.filter(spu_id=spu_id)

    serializer_class = SpecOptionSerializer


# 查询规格选项表
# class SpecOptView(ListAPIView):
#     queryset = SpecificationOption.objects.all()
#     serializer_class = SpecOptSerializer
#     pagination_class = MeiduoPagination


# 获取品牌信息
class SpecSimpleView(ListAPIView):
    queryset = SPUSpecification.objects.all()
    serializer_class = SpecSimpleSerializer


# 保存规格选项数据
class SpecSaveViewSet(ModelViewSet):
    queryset = SpecificationOption.objects.all().order_by('-id')
    serializer_class = SpecSaveSerializer
    pagination_class = MeiduoPagination
